﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;

namespace FBGraph
{
    /// <summary>Contains extension methods for <see cref="IDictionary{TKey, TValue}" /> implementations.</summary>
    public static class DictionaryExtensions
    {
        /// <summary>Converts a <see cref="IDictionary{TKey, TValue}" /> to a <see cref="NameValueCollection" />.</summary>
        /// <typeparam name="TKey">The type of keys in the dictionary.</typeparam>
        /// <typeparam name="TValue">The type of values in the dictionary.</typeparam>
        /// <param name="dict">The dictionary to convert.</param>
        /// <returns>A <see cref="NameValueCollection" /> instance containing the keys and values contained in the specified dictionary.</returns>
        public static NameValueCollection ToNameValueCollection<TKey, TValue>(this IDictionary<TKey, TValue> dict)
        {
            if (dict == null) return null;
            else
            {
                var nvc = new NameValueCollection(dict.Count);
                foreach (var entry in dict)
                {
                    nvc.Add(entry.Key.ToString(), entry.Value == null ? null : entry.Value.ToString());
                }
                return nvc;
            }
        }
    }
}